package com.huanuo.internetofthings.service.fastStatistics;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huanuo.internetofthings.bo.fastStatistics.FastStatistics;
import com.huanuo.internetofthings.dao.fastStatistics.FastStatisticsMapper;
import com.huanuo.internetofthings.dto.fastStatistics.FastStatisticsPageRequestDto;
import com.huanuo.internetofthings.dto.fastStatistics.FastStatisticsResDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.unicorn.framework.base.base.AbstractService;
import org.unicorn.framework.core.ResponseDto;
import org.unicorn.framework.core.SysCode;
import org.unicorn.framework.core.exception.PendingException;

import java.util.Comparator;
import java.util.List;
/**
 * <p>
 * fast数据统计表 服务实现类
 * </p>
 *
 * @author xiebin
 * @since 2018-07-20
 */
@Service
public class FastStatisticsService extends AbstractService {
  @Autowired
  private FastStatisticsMapper fastStatisticsMapper;
  /**
     * 根据主键删除记录
     * @param id
     * @return
     * @throws PendingException
     */
  public void deleteByPrimaryKey(Integer id) throws PendingException{
    try {
			 fastStatisticsMapper.deleteByPrimaryKey(id);
		} catch (Exception e) {
			error("删除记录失败", e);
			throw new PendingException(SysCode.DB_ERROR, "删除记录失败");
	}
    
  }


   /**
     * 保存记录
     * @param record
     * @return
     * @throws PendingException
     */
   public  FastStatistics save(FastStatistics record) throws PendingException{
       try {
			 fastStatisticsMapper.insertSelective(record);
			 return record;
		} catch (Exception e) {
			error("新增记录失败", e);
			throw new PendingException(SysCode.DB_ERROR, "新增记录失败");
	}
   }
      /**
     * 根据主键查询记录
     * @param id
     * @return
     * @throws PendingException
     */
    public FastStatistics selectByPrimaryKey(Integer id) throws PendingException{
    try {
			return fastStatisticsMapper.selectByPrimaryKey(id);
		} catch (Exception e) {
			error("查询记录失败", e);
			throw new PendingException(SysCode.DB_ERROR, "查询记录失败");
	}
    }
    
     /**
     * 根据条件查询记录列表
     * @param record
     * @return
     * @throws PendingException
     */
    public  List<FastStatistics> selectList(FastStatistics record) throws PendingException{
       try {
			return fastStatisticsMapper.selectList(record);
		} catch (Exception e) {
			error("查询列表失败", e);
			throw new PendingException(SysCode.DB_ERROR, "查询列表失败");
	}
    }
    
    /** 
	 * 分页查询fast数据统计表信息
	 * 
	 * @param fastStatisticsPageRequestDto
	 * @return
	 * @throws PendingException
	 */
	public PageInfo<FastStatistics> getFastStatisticsByPage(FastStatisticsPageRequestDto fastStatisticsPageRequestDto)
			throws PendingException {
		try {
			PageHelper.startPage(fastStatisticsPageRequestDto.getPageNo(),fastStatisticsPageRequestDto.getPageSize());
			List<FastStatistics> list=fastStatisticsMapper.selectList(fastStatisticsPageRequestDto.getFastStatistics());
			PageInfo<FastStatistics> pageInfo = new PageInfo<>(list);
			return pageInfo;
		}catch (Exception e) {
			error("分页查询fast数据统计表失败", e);
			throw new PendingException(SysCode.DB_ERROR, "分页查询fast数据统计表失败");
		}
	}
    
    /**
     * 根据主键列表查询记录
     * @param ids
     * @return
     * @throws PendingException
     */
    public  List<FastStatistics> selectListByIds(List<Integer> ids) throws PendingException{
     try {
			return fastStatisticsMapper.selectListByIds(ids);
		} catch (Exception e) {
			error("根据ID列表批量查询记录失败", e);
			throw new PendingException(SysCode.DB_ERROR, "根据ID列表批量查询记录失败");
	}
    }
   /**
     * 根据条件查询一条记录
     * @param record
     * @return
     * @throws PendingException
     */
    public  FastStatistics selectOne(FastStatistics record) throws PendingException{
      try {
			return fastStatisticsMapper.selectOne(record);
		} catch (Exception e) {
			error("查询单条记录失败", e);
			throw new PendingException(SysCode.DB_ERROR, "查询单条记录失败");
	}
    }

    /**
     * 根据主键修改记录
     * @param record
     * @return
     * @throws PendingException
     */
    public FastStatistics updateByPrimaryKey(FastStatistics record) throws PendingException{
       try {
			 fastStatisticsMapper.updateByPrimaryKey(record);
			return record;
		} catch (Exception e) {
			error("根据主键更新单条记录失败", e);
			throw new PendingException(SysCode.DB_ERROR, "根据主键更新单条记录失败");
	}
    }

	/**
	 * 查询Fast数据信息
	 * @param record
	 * @return
	 * @throws PendingException
	 */
    public ResponseDto<FastStatisticsResDto> queryInfo(FastStatistics record) throws PendingException{
    	try{
			FastStatisticsResDto fast=new FastStatisticsResDto();
			//查询所有设备统计信息
			List<FastStatistics> rankList=fastStatisticsMapper.selectList(record);
			//设置设备总数以及在网总数
			fast.setDevide_total(rankList.stream().mapToInt(FastStatistics::getDevideTotal).sum());
			fast.setDevice_online_total(rankList.stream().mapToInt(FastStatistics::getDeviceOnlineTotal).sum());
			//倒序排序
			rankList.sort(new Comparator<FastStatistics>() {
				@Override
				public int compare(FastStatistics o1, FastStatistics o2) {
					return o2.getDevideTotal().compareTo(o1.getDevideTotal());
				}
				
			});
			fast.setList(rankList);
			return new ResponseDto<>(fast);
		}catch (Exception e){
    		error("查询fast数据失败", e);
    		throw new PendingException(SysCode.DB_ERROR, "查询fast数据失败");
		}

    }
}
